package com.ten_4_4.hiring.talent.dao;

import java.util.ArrayList;
import java.util.List;

import com.ten_4_4.hiring.comm.util.DBUtil;
import com.ten_4_4.hiring.talent.po.TalentPo;

public class TalentDao {

	public TalentPo login(String code, String password) {
		String sql = "select * from talent where code = ? and password = ?";
		return DBUtil.getUniqueResult(TalentPo.class, sql, code, password);
	}

	public TalentPo register(String code, String password, String nickname) {
		String sql = "insert into talent(code, password, nickname) values(?, ?, ?)";
		int row = DBUtil.executeUpdate(sql, code, password, nickname);
		if(row == 1) {
			sql = "select * from talent where code = ? and password = ?";
			return DBUtil.getUniqueResult(TalentPo.class, sql, code, password);
		}
		return null;
	}

	public int modifyPassword(int talentId, String password) {
		String sql = "update talent set password = ? where id = ?";
		return DBUtil.executeUpdate(sql, password, talentId);
	}

	public int modifyEmail(int talentId, String email) {
		String sql = "update talent set email = ? where id = ?";
		return DBUtil.executeUpdate(sql, email, talentId);
	}

	public int modifyPhone(int talentId, String phone) {
		String sql = "update talent set phone = ? where id = ?";
		return DBUtil.executeUpdate(sql, phone, talentId);
	}

	public int modifyNickname(int talentId, String nickname) {
		String sql = "update talent set nickname = ? where id = ?";
		return DBUtil.executeUpdate(sql, nickname, talentId);
	}
	public int saveBaseInfo(TalentPo talentPo) {
		StringBuffer sb = new StringBuffer();
		sb.append("update talent set ");
		sb.append("name = ?, age = ?, hobby = ?, gender = ? ");
		sb.append("where id = ? ");
		List<Object> paramList = new ArrayList<>();
		paramList.add(talentPo.getName());
//		paramList.add(talentPo.getPhone());
//		paramList.add(talentPo.getEmail());
		paramList.add(talentPo.getAge());
		paramList.add(talentPo.getHobby());
		paramList.add(talentPo.getGender());
		paramList.add(talentPo.getId());
		return DBUtil.executeUpdate(sb.toString(), paramList);
	}

	public TalentPo findTalentByEmail(String oldEmail) {
		String sql = "select id from talent where email = ? ";
		TalentPo talentPo = DBUtil.getUniqueResult(TalentPo.class, sql, oldEmail);
		return talentPo;
	}

}
